package mylog

import (
	"fmt"
	"log"
	"os"
)

const (
	V = iota
	D
	I
	W
	E
)

var logger *log.Logger
var logLevel = V

func InitLogger() {
	logFile, err := os.OpenFile("./log.txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, os.ModePerm)
	if err != nil {
		log.Println("open log file error")
	}
	logger = log.New(logFile, "\r\n", log.Ldate|log.Ltime|log.Lshortfile)
}

func SetLogLevel(level int) {
	logLevel = level
}

func Log(v ...interface{}) {
	logger.Println(v)
}

func LogE(v ...interface{}) {
	logger.Println("E", v)
	fmt.Println(v)
}

func LogW(v ...interface{}) {
	if logLevel <= W {
		logger.Println("W", v)
	}
	fmt.Println(v)
}

func LogI(v ...interface{}) {
	if logLevel <= I {
		Log("I", v)
	}
	fmt.Println(v)
}

func LogD(v ...interface{}) {
	if logLevel <= D {
		Log("D", v)
	}
	fmt.Println(v)
}

func LogV(v ...interface{}) {
	if logLevel <= V {
		Log("V", v)
	}
	fmt.Println(v)
}

func LogOneLine() {
	logger.Println("------------------------------------------------------------------------------------------------")
}

func LogTwoLine() {
	logger.Println("================================================================================================")
}

func WrapWithSingleLine(v ...interface{}) {
	LogOneLine()
	logger.Println(v)
	LogOneLine()
}

func WrapWithDoubleLine(v ...interface{}) {
	LogTwoLine()
	logger.Println(v)
	LogTwoLine()
}
